<?php
function check_email($email){
	$regexp = "^[a-zA-Z0-9\-_\.]+\@[a-zA-Z0-9_\.\-]+(\.)(com|net|cn|cc|ch|jp|kr|tw|hk|sg|my)$";
	return eregi($regexp, $email);
}

function cstrlen($str)  {
	$strlen = strlen($str);
    $aStrlen = 0;
    for($i=0;$i<$strlen;$i++)  {
		if(ord(substr($str,$i,1))>0xa0)  {
			$aStrlen += 2;
			$i = $i + 2;
		}  else
			$aStrlen += 1;
   }
   return  $aStrlen;
}
function csubstr($str,$start,$len)  {
       $strlen=$start+$len;
       for($i=0;$i<$strlen;$i++)  {
               if(ord(substr($str,$i,1))>0xa0)  {
                       $tmpstr.=substr($str,$i,2);
                       $i++;
               }  else
                       $tmpstr.=substr($str,$i,1);
       }
       return  $tmpstr;
}

function utf8_to_gbk($str){
	return iconv("UTF8","GBK",$str);
}

function gbk_to_utf8($str){
	return iconv("GBK","UTF8",$str);
}

function truncate($str, $len, $tail = "..."){
	if (strlen($str) > $len){
		$ret = substr($str, 0, $len);
		if (ord(substr($ret, -1,1)) > 0xa0){
			$ret[strlen($ret) - 1] = " ";
		}
		$ret .= $tail;
	}else{
		return $str;
	}
}

function is_chinese($str){
	return (preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", utf8_to_gbk($str)));
}

function escape_string($str){
	if (get_magic_quotes_gpc()){
		return $str;
	}else{
		return mysql_escape_string($str);
	}
}
function array_dump($array){
	$return = "";
	foreach($array as $k => $v){
		if (is_array($v)){
			$return .= "[".$k."]=>{\n";
			$return .= array_dump($v)."\n";
			$return .= "}\n";
		}else{
			$return .= $k." => ".$v."\n";
		}
	}
	return $return;
}

function get_microtime(){
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

/**
 * url导向
 *
 * @param str url	地址
 * @return void
**/
function url_redirect($url) {
	if($url != "") {
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>';
		echo '<script language="javaScript">';
		echo 'window.location = "'.$url.'";';
		echo '</script>';
		echo '</body></html>';
	}
}
/**
 * 返回信息导向
 *
 * @param str msg	信息
 * @param str url	地址
 * @return void
**/
function url_msg_redirect($msg,$url) {
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>';
	echo '<script language="javaScript">';
	echo 'alert("'.$msg.'");';
	echo 'window.location = "'.$url.'";';
	echo '</script>';
	echo '</body></html>';
}
/**
 * 获取客户端ip
 *
 * @return string
**/
function get_client_ip() {
	$user_ip = "";
	if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
		$user_ip = getenv('HTTP_CLIENT_IP');
	} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
		$user_ip = getenv('HTTP_X_FORWARDED_FOR');
	} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
		$user_ip = getenv('REMOTE_ADDR');
	} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
		$user_ip = $_SERVER['REMOTE_ADDR'];
	}
	return $user_ip;
}


function get_user_db_hash($player_id, $length = 1){
	if ($length < 1 || $length > 32){
		return false;
	}

	return "_".substr(md5($player_id), 0, $length);
}

function get_double_hex_key($id) {
	$num = intval($id);
	if($num <= 0)
		return "t";
	$rand_str	= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	$dec    = strlen($rand_str);

	$str = "";
	while(true) {
		if($num <= $dec) {
			if($num > 0)
				$str = $rand_str[$num - 1] . $str;
			break;
		} else {
			$remainder = $num % $dec;
			if($remainder > 0)
				$str = $rand_str[$remainder - 1] . $str;
			else
				$str = "0" . $str;

			$num = floor($num / $dec);
		}
	}
	return $str;
}

function output($raw_data,$cmd1,$cmd2){
	$len = strlen($raw_data) + strlen(pack("CC",0,0));
	//$len = 0x80000000 | $len;
	//echo "raw_data:".$raw_data."len:".$len;exit;
	$output = pack("NCC",$len, $cmd1, $cmd2).$raw_data;
	/*
	if($cmd1 == 6 && $cmd2 == 18) {
		$arr = unpack("Nint", $raw_data);
		wlog("newer",json_encode($arr));
	}
	*/
	return $output;
}

/*number(0-100)*/
function cal_odds($number)
{
	$odds_figure = 1000000;
	if($number >= 100) {
		return true;
	} else if($number <= 0) {
		return false;
	}

	$iRand		= floor(rand($odds_figure,100*$odds_figure)/$odds_figure);

	if($iRand <= $number) {
		return true;
	} else {
		return false;
	}
}
function cal_rand($per = 100)
{
	return rand(0,$per);
	/*
	$odds_figure = 1000000;

	$iRand = floor(rand($odds_figure,$per*$odds_figure)/$odds_figure);

	return $iRand;
	*/
}
function check_event($event)
{
	if(!empty($event)) {
		$objEventMode = json_decode($event);

		if(isset($objEventMode->type) && $objEventMode->type != 2)
			return true;
	}
	return false;
}
function pt(){
	$parm = func_get_args();
	if(!empty($parm)){
		echo '<pre>';
		foreach ($parm as $value){
			print_r($value);
			echo '<br />';
		}
		die();
	}
	echo 'parm empty!';
}

//$number 将要向上取的数,div向上取的数值
function ceilUp($number,$div){
	if(is_numeric($number) && is_numeric($div)){
	   return ceil($number/$div)*$div;
	}else{
		return false;
	}
}

function get_option($idx=-1){
	$arrData=null;
	if($idx == -1){
		$arrData = array();
	}elseif($idx == 0){
		$arrData = $_SESSION['soption'];
	}elseif(!empty($_SESSION['funOption']) && $idx>0){
		$arrData = $_SESSION['funOption'][$idx];
		$_SESSION['soption'] = $arrData;
	}
	return $arrData;
}

// 写入缓存
function writetocache($script, $cachedata = '', $prefix = 'cache_') {
		$dir = ROOT_PATH.'/tmp/';
		if(!is_dir($dir)) {
			@mkdir($dir, 0777);
		}
		$authkey = 'Dream';
		if($fp = @fopen("$dir$prefix$script.php", 'wb')) {
			fwrite($fp, "<?php\n//CANGHAI! cache file, DO NOT modify me!".
				"\n//Created: ".date("M j, Y, G:i").
				"\n//Identify: ".md5($prefix.$script.'.php'.$cachedata.$authkey)."\n\n$cachedata?>");
			fclose($fp);
		} else {
			exit('Can not write to cache files, please check directory /cache/activity/');
		}
}

/**
 * 获取目录下文件列表
 */

//获取文件目录列表,该方法返回数组
function get_dir_files($dir) {
	$dirArray[]=NULL;
	if (false != ($handle = opendir ( $dir ))) {
		$i=0;
		while ( false !== ($file = readdir ( $handle )) ) {
			if ($file != "." && $file != ".." && strpos($file,".")) {
				if($arrInfo = explode('.',$file)){
					$dirArray[$i]=$arrInfo[0];
					$i++;
				}
			}
		}
		//关闭句柄
		closedir ( $handle );
	}
	return $dirArray;
}

?>